﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Data.Interfaces;

namespace Dost.Khcn.Data.Implements
{
    public class FileDao : IFileDao
    {
        public List<ChuyenGiaFileView> GetFiles(int chuyenGiaId)
        {
            using (var db = new NLKHCNDataEntities())
            {
                return db.ChuyenGiaFileViews.Where(x => x.ChuyenGiaId == chuyenGiaId).ToList();
            }
        }

        public ChuyenGiaFile GetFileById(int id)
        {
            using (var db = new NLKHCNDataEntities())
            {
                return db.ChuyenGiaFiles.FirstOrDefault(x => x.Id == id);
            }
        }

        public void Create(ChuyenGiaFile chuyenGiaFile)
        {
            if (chuyenGiaFile == null) return;

            using (var db = new NLKHCNDataEntities())
            {
                db.ChuyenGiaFiles.Add(chuyenGiaFile);
                db.SaveChanges();
            }
        }

        public void Delete(int id)
        {
            if (id <= 0) return;

            using (var db = new NLKHCNDataEntities())
            {
                var file = new ChuyenGiaFile {Id = id};
                db.ChuyenGiaFiles.Attach(file);
                db.Entry(file).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }
    }
}
